﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Management; 
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.Resources;
using DukeySoundHelper.API;

namespace DukeySoundHelper
{
    public partial class RegisterFrm : Form
    {
        public static RegisterFrm instance = new RegisterFrm();
        public RegisterFrm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {            
            string codeSource=txtCodeSource.Text;//机器硬编码
            string signKey=txtCode.Text;//注册码
            if(CryptRSA.CheckAuth(codeSource,signKey,DukeyComm.pubKey))
            {
                MessageBox.Show("Congratulations! Registered successfully","Congratulations");
                this.DialogResult = DialogResult.OK;
               
                //保存注册信息到资源文件     
                StreamWriter sw = new StreamWriter("auth.dat", false);
                sw.Write(signKey);
                sw.Close();

                this.Close();
            }            
            else
            {
                lblInfo.Text = "Error Code!";
            }
        }

        private void RegisterFrm_Load(object sender, EventArgs e)
        {
            //加载注册申请码
            txtCodeSource.Text =WMICardGrab.GetCPUId();
        }

        /// <summary>
        /// 发送邮件给我
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMail_Click(object sender, EventArgs e)
        {
            btnSendMail.Enabled = false;
            string smtpServer = "smtp.qq.com";
            int smtpPort = 25;
            string from = "1360406246@qq.com";
            string password = "zhongjian";
            string to = "1360406246@qq.com";           
           
            string htmlBody = "机器码:<br>" + txtCodeSource.Text + "<br><br>from:"+txtYourEmail.Text+"<br>来自客户端:"+WMICardGrab.GetClientsysInfo();
            try
            {
                DukeyComm.SendSMTPEMail(smtpServer, smtpPort, from, password, to, "申请都客音量调节助手注册码(" + txtYourEmail.Text + ")", htmlBody);
                MessageBox.Show("Send Mail Success!,");
                btnSendMail.Enabled = true;
            }
            catch { MessageBox.Show("Fail!"); btnSendMail.Enabled = true;}
            
        }
    }
}
